package protocol.SZ_2021;

import java.util.HashMap;

public class Handle2021SZ86 implements IHandle2021SZ {
    @Override
    public String handle(SZ_2021Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x00:
                return handle8600(cmd);
            case 0x01:
                return handle8601(cmd);
            case 0x02:
                return handle8602(cmd);
            case 0x03:
                return handle8603(cmd);
            case 0x04:
                return handle8604(cmd);
            case 0x05:
                return handle8605(cmd);
            case 0x06:
                return handle8606(cmd);
            case 0x07:
                return handle8607(cmd);
            case 0x0A:
                return handle860A(cmd);
            default:
                return "深标:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle860A(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:查询区域线路");
        builder.append("\r\n");
        return builder.toString();
    }

    private String handle8607(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除线路");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWord());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8606(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置线路");
        builder.append("\r\n");

        builder.append("线路ID:"+cmd.readDWord());
        builder.append("\r\n");
        int value = cmd.readWord();
        builder.append("线路属性:"+value);
        builder.append("\r\n");

        boolean isByTime = (value&0x01) == 1;
        builder.append("    根据时间(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进线路报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出线路报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");

        if (isByTime){
            builder.append("起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");
        }

        int num = cmd.readWord();
        builder.append("路线拐点项数:"+num);
        builder.append("\r\n");
        for (int i = 0; i < num; i++) {
            builder.append("    拐点ID:"+cmd.readDWord());
            builder.append("\r\n");
            builder.append("    路段ID:"+cmd.readDWord());
            builder.append("\r\n");

            builder.append("    拐点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    拐点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    路段宽度:"+cmd.readInt());
            builder.append("\r\n");
            int value2 = cmd.readWord();
            builder.append("    路段属性:"+value2);
            builder.append("\r\n");

            boolean isLimitTime = (value&0x01) == 1;
            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        根据时间(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>3&0x01));
            builder.append("\r\n");
            boolean isExtra = (value>>4&0x01) == 1;//是否有拓展信息
            builder.append("        0-无扩展属性,1-有扩展属性:"+(value>>4&0x01));
            builder.append("\r\n");

            if (isLimitTime){
                builder.append("    路段行驶过长阈值:"+cmd.readWord());
                builder.append("\r\n");

                builder.append("    路段行驶不足阈值:"+cmd.readWord());
                builder.append("\r\n");
            }

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWord());
                builder.append("\r\n");

                builder.append("    路段超速持续时间:"+cmd.readWord());
                builder.append("\r\n");
            }

            if (isExtra){
                int size = cmd.readWord();
                builder.append("    围栏扩展数据，拓展属性个数"+size);
                builder.append("\r\n");
                for (int j = 0; j < size; j++) {
                    int extID = cmd.readInt();
                    int extLen = cmd.readInt();
                    if (extID == 0x01){
                        builder.append("        围栏限速提醒值，单位km/h:"+cmd.readInt(extLen));
                        builder.append("\r\n");
                    }else{
                        builder.append("        未定义编码:"+extID+",内容"+cmd.readBCD(extLen));
                        builder.append("\r\n");
                    }
                }
            }

        }

        return builder.toString();
    }

    private String handle8605(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除多边形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWord());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8604(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置多边形区域");
        builder.append("\r\n");

        builder.append("区域ID:"+cmd.readDWord());
        builder.append("\r\n");
        int value = cmd.readWord();
        builder.append("属性:"+value);
        builder.append("\r\n");

        boolean isLimitSpeed = (value>>1&0x01) == 1;
        builder.append("    根据时间(1有效):"+(value&0x01));
        builder.append("\r\n");
        builder.append("    限速(1有效):"+(value>>1&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给驾驶员(1有效)"+(value>>2&0x01));
        builder.append("\r\n");
        builder.append("    进区域报警给平台(1有效):"+(value>>3&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给驾驶员(1有效):"+(value>>4&0x01));
        builder.append("\r\n");
        builder.append("    出区域报警给平台:"+(value>>5&0x01));
        builder.append("\r\n");
        builder.append("    0-北纬,1-南纬:"+(value>>6&0x01));
        builder.append("\r\n");
        builder.append("    0-东经,1-西经:"+(value>>7&0x01));
        builder.append("\r\n");
        builder.append("    0-允许开门,1-禁止开门:"+(value>>8&0x01));
        builder.append("\r\n");
        builder.append("    0-普通围栏,1-城区围栏:"+(value>>9&0x01));
        builder.append("\r\n");
        builder.append("    热点商圈广告围栏(1有效):"+(value>>10&0x01));
        builder.append("\r\n");

        int temp = value >> 11&0x03;//12-11位为围栏信息
        boolean isJJYWL = temp == 1;//是否为计价用围栏
        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"普通围栏");
        list.put(1,"计价用围栏，区域信息后附加计价信息");
        list.put(2,"收费站围栏");

        builder.append("    围栏信息："+list.getOrDefault(temp,"未定义"));
        builder.append("\r\n");

        boolean isExtra = (value>>13&0x01) == 1;//是否有拓展信息
        builder.append("    0-无扩展属性,1-有扩展属性:"+(value>>13&0x01));
        builder.append("\r\n");

        builder.append("    0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
        builder.append("\r\n");

        builder.append("    0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
        builder.append("\r\n");

        int pointNum = cmd.readWord();
        builder.append("顶点数量:"+pointNum);
        builder.append("\r\n");
        for (int i = 0; i < pointNum; i++) {
            builder.append("    顶点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    顶点经度:"+cmd.readInt(4));
            builder.append("\r\n");
        }

        if (isJJYWL){
            builder.append("    区域计价信息（过桥费-xxxx.xx元）:"+cmd.readBCD(3));
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle8603(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除矩形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWord());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8602(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置矩形区域");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"更新区域");
        list.put(1,"追加区域");
        list.put(2,"修改区域");
        builder.append("设置属性:"+list.getOrDefault(cmd.readInt(),"未定义"));
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:"+cmd.readInt(4));
            builder.append("\r\n");
            int value = cmd.readWord();
            builder.append("    属性:"+value);
            builder.append("\r\n");

            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        根据时间(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给驾驶员(1有效)"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给平台(1有效):"+(value>>3&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给驾驶员(1有效):"+(value>>4&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给平台:"+(value>>5&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>6&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>7&0x01));
            builder.append("\r\n");
            builder.append("        0-允许开门,1-禁止开门:"+(value>>8&0x01));
            builder.append("\r\n");
            builder.append("        0-普通围栏,1-城区围栏:"+(value>>9&0x01));
            builder.append("\r\n");
            builder.append("        热点商圈广告围栏(1有效):"+(value>>10&0x01));
            builder.append("\r\n");

            int temp = value >> 11&0x03;//12-11位为围栏信息
            boolean isJJYWL = temp == 1;//是否为计价用围栏
            list.clear();
            list.put(0,"普通围栏");
            list.put(1,"计价用围栏，区域信息后附加计价信息");
            list.put(2,"收费站围栏");

            builder.append("        围栏信息："+list.getOrDefault(temp,"未定义"));
            builder.append("\r\n");

            boolean isExtra = (value>>13&0x01) == 1;//是否有拓展信息
            builder.append("        0-无扩展属性,1-有扩展属性:"+(value>>13&0x01));
            builder.append("\r\n");

            builder.append("        0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
            builder.append("\r\n");

            builder.append("        0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
            builder.append("\r\n");

            builder.append("    左上点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    左上点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    右下点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    右下点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("    结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWord());
                builder.append("\r\n");

                builder.append("    超速持续时间:"+cmd.readInt());
                builder.append("\r\n");
            }

            if (isJJYWL){
                builder.append("    区域计价信息（过桥费-xxxx.xx元）:"+cmd.readBCD(3));
                builder.append("\r\n");
            }

            if (isExtra){
                int size = cmd.readWord();
                builder.append("    围栏扩展数据，拓展属性个数"+size);
                builder.append("\r\n");
                for (int j = 0; j < size; j++) {
                    int extID = cmd.readInt();
                    int extLen = cmd.readInt();
                    if (extID == 0x01){
                        builder.append("        围栏限速提醒值，单位km/h:"+cmd.readInt(extLen));
                        builder.append("\r\n");
                    }else{
                        builder.append("        未定义编码:"+extID+",内容"+cmd.readBCD(extLen));
                        builder.append("\r\n");
                    }

                }
            }
        }
        return builder.toString();
    }

    private String handle8601(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:删除圆形区域");
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:" + cmd.readDWord());
            builder.append("\r\n");
        }
        return builder.toString();
    }

    private String handle8600(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:设置圆形区域");
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"更新区域");
        list.put(1,"追加区域");
        list.put(2,"修改区域");
        builder.append("设置属性:"+list.getOrDefault(cmd.readInt(),"未定义"));
        builder.append("\r\n");

        int num = cmd.readInt();
        builder.append("区域总数:"+num);
        for (int i = 0; i < num; i++) {
            builder.append("    区域ID:"+cmd.readInt(4));
            builder.append("\r\n");
            int value = cmd.readWord();
            builder.append("    属性:"+value);
            builder.append("\r\n");

            boolean isLimitSpeed = (value>>1&0x01) == 1;
            builder.append("        根据时间(1有效):"+(value&0x01));
            builder.append("\r\n");
            builder.append("        限速(1有效):"+(value>>1&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给驾驶员(1有效)"+(value>>2&0x01));
            builder.append("\r\n");
            builder.append("        进区域报警给平台(1有效):"+(value>>3&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给驾驶员(1有效):"+(value>>4&0x01));
            builder.append("\r\n");
            builder.append("        出区域报警给平台:"+(value>>5&0x01));
            builder.append("\r\n");
            builder.append("        0-北纬,1-南纬:"+(value>>6&0x01));
            builder.append("\r\n");
            builder.append("        0-东经,1-西经:"+(value>>7&0x01));
            builder.append("\r\n");
            builder.append("        0-允许开门,1-禁止开门:"+(value>>8&0x01));
            builder.append("\r\n");
            builder.append("        0-普通围栏,1-城区围栏:"+(value>>9&0x01));
            builder.append("\r\n");
            builder.append("        热点商圈广告围栏(1有效):"+(value>>10&0x01));
            builder.append("\r\n");

            int temp = value >> 11&0x03;//12-11位为围栏信息
            boolean isJJYWL = temp == 1;//是否为计价用围栏
            list.clear();
            list.put(0,"普通围栏");
            list.put(1,"计价用围栏，区域信息后附加计价信息");
            list.put(2,"收费站围栏");

            builder.append("        围栏信息："+list.getOrDefault(temp,"未定义"));
            builder.append("\r\n");

            boolean isExtra = (value>>13&0x01) == 1;//是否有拓展信息
            builder.append("        0-无扩展属性,1-有扩展属性:"+(value>>13&0x01));
            builder.append("\r\n");

            builder.append("        0-进区域开启通信模块,1-进区域关闭通信模块:"+(value>>14&0x01));
            builder.append("\r\n");

            builder.append("        0-进区域不采集 GNSS 详细定位数据,1-进区域采集 GNSS 详细定位数据:"+(value>>15&0x01));
            builder.append("\r\n");

            builder.append("    中心点纬度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    中心点经度:"+cmd.readInt(4));
            builder.append("\r\n");

            builder.append("    半径:"+cmd.readDWord());
            builder.append("\r\n");

            builder.append("    起始时间:"+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("    结束时间:"+cmd.readBCD(6));
            builder.append("\r\n");

            if (isLimitSpeed){
                builder.append("    最高速度（超速限值）:"+cmd.readWord());
                builder.append("\r\n");

                builder.append("    超速持续时间:"+cmd.readInt());
                builder.append("\r\n");
            }

            if (isJJYWL){
                builder.append("    区域计价信息（过桥费-xxxx.xx元）:"+cmd.readBCD(3));
                builder.append("\r\n");
            }

            if (isExtra){
                int size = cmd.readWord();
                builder.append("    围栏扩展数据，拓展属性个数"+size);
                builder.append("\r\n");
                for (int j = 0; j < size; j++) {
                    int extID = cmd.readInt();
                    int extLen = cmd.readInt();
                    if (extID == 0x01){
                        builder.append("        围栏限速提醒值，单位km/h:"+cmd.readInt(extLen));
                        builder.append("\r\n");
                    }else{
                        builder.append("        未定义编码:"+extID+",内容"+cmd.readBCD(extLen));
                        builder.append("\r\n");
                    }

                }
            }
        }

        return builder.toString();
    }

}
